import numpy as np

from jili.calc import calcor_base
class zscore(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["zscore"]
        self.input = ["close"]
        self.parameters = {"timeperiod":60}
        super().__init__(ta_arg)
        self.batch=self.parameters["timeperiod"]
        self.lookback = self.batch - 1
        # if "mintimeperiod" in self.parameters.keys():

    def calc(self):
        data0=self.hisbars[self.input[0]]
        m0=np.mean(data0)
        std0=np.std(data0)
        if np.isnan(std0):
            zscore=None
        else:
            if std0!=0:
                zscore = (data0[-1]-m0)/std0
            else:
                zscore = None
        return zscore
class zscore_norm(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["zscore"]
        self.input = ["close"]
        self.parameters = {"timeperiod":60}
        super().__init__(ta_arg)
        self.batch=self.parameters["timeperiod"]
        # if "mintimeperiod" in self.parameters.keys():

    def calc(self):
        data0=self.hisbars[self.input[0]]
        m0=np.median(data0)
        p25=np.percentile(data0,25)
        p75=np.percentile(data0,75)
        std0=p75-p25
        if np.isnan(std0):
            zscore=None
        else:
            if std0 != 0:
                zscore = (data0[-1]-m0)/std0
            else:
                zscore = None
        return zscore